package rsax

import (
	"crypto"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"encoding/hex"
	"testing"
)

func TestSha1Sign(t *testing.T) {
	priKey, err := PemPrivateKey([]byte(privateKeyStr))
	if err != nil {
		t.Error(err)
	}

	signature, err := Sha1Sign(priKey, []byte(plainText))
	if err != nil {
		t.Error(err)
	}

	t.Log(hex.EncodeToString(signature))
}

func TestSha1Verify(t *testing.T) {
	sig := "6476c4ee67a0e54d03fffcb0bbd55c44ad4dc748dc6f3cf9762f55f061a357fdcfd88d4710ef4b84b676bd732bda233d199b4404f7a9cb2ae41b5f2215bde404abf5fd0689c3152d5f516e5495b49275d5aa1587b1aeb5251ebd885b1ad1cc6fa5219565a7f2ee02e0b567f802bbe5012b9f6790becc325b6cc5a517b1b49e16"

	pubKey, err := PemPublicKey([]byte(publicKeyStr))
	if err != nil {
		t.Error(err)
	}

	raw, err := hex.DecodeString(sig)
	if err != nil {
		t.Error(err)
	}

	hashed := sha1.Sum([]byte(plainText))

	err = Sha1Verify(pubKey, hashed[:], raw)
	if err != nil {
		t.Error(err)
	}
}

func TestSha256Sign(t *testing.T) {
	priKey, err := PemPrivateKey([]byte(privateKeyStr))
	if err != nil {
		t.Error(err)
	}

	signature, err := Sha256Sign(priKey, []byte(plainText))
	if err != nil {
		t.Error(err)
	}

	t.Log(hex.EncodeToString(signature))
}

func TestSha256Verify(t *testing.T) {
	sig := "6a55cef9b21a5f59d38642d23ee24b4bc76b6ae7eae346e4f23a62abe353dcac6c6e968bded01a37856f7e98b494aa9d8c495791da1fb42b4ff6a30cfbff962a1185ac5fa2775b6b992d9214cb98fcf110bb00b384a43ff7c1c654ba0f557de66ab6c5802f8c0f5caa98c6903e87252998a2d713af9086c1dfd62fe45be8ac25"

	pubKey, err := PemPublicKey([]byte(publicKeyStr))
	if err != nil {
		t.Error(err)
	}

	raw, err := hex.DecodeString(sig)
	if err != nil {
		t.Error(err)
	}

	hashed := sha256.Sum256([]byte(plainText))

	err = Sha256Verify(pubKey, hashed[:], raw)
	if err != nil {
		t.Error(err)
	}
}

func TestSign(t *testing.T) {
	priKey, err := PemPrivateKey([]byte(privateKeyStr))
	if err != nil {
		t.Error(err)
	}

	hashed := sha512.Sum512([]byte(plainText))

	signature, err := Sign(priKey, crypto.SHA512, hashed[:])
	if err != nil {
		t.Error(err)
	}

	t.Log(hex.EncodeToString(signature))
}

func TestVerify(t *testing.T) {
	sig := "004f314bac4e5723f079788f37b46076d36e26cd0583ff43744e9f9ec2ad14f56edacbe38b5dac63979a53aec9d7300e43fcb78f1eee1130e2eb0576a27a3228870351f81ee192efce2847a599811c8d66e16f92705cc54e9f8b105ea9d6b9752ce3d8f9c93f927aaeae8cd9d6dc3688865ad16b14681b331020e7a2a214f658"

	pubKey, err := PemPublicKey([]byte(publicKeyStr))
	if err != nil {
		t.Error(err)
	}

	raw, err := hex.DecodeString(sig)
	if err != nil {
		t.Error(err)
	}

	hashed := sha512.Sum512([]byte(plainText))

	err = Verify(pubKey, crypto.SHA512, hashed[:], raw)
	if err != nil {
		t.Error(err)
	}
}
